package com.springboot.chongwu.controller.app;


import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.excel.util.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.chongwu.entity.Yonghu;
import com.springboot.chongwu.satoken.StpUserUtil;
import com.springboot.chongwu.service.IYonghuService;
import com.springboot.chongwu.util.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-09-24
 */
@Validated
@RestController
@RequestMapping("/api/front/yonghu")
public class AppYonghuController {

    @Autowired
    private IYonghuService iYonghuService;

    /**
     * 前端用户登录
     * @param account
     * @param mima
     * @return
     */
    @PostMapping("/login")
    public ResultView adLogin(@NotNull(message = "账号不能为空") String account,
                              @NotNull(message = "密码不能为空") String mima){
        //进行登录认证
        Yonghu yonghu=this.iYonghuService.login(account,mima);
        if(yonghu==null){
            return ResultView.error("用户不存在！！！");
        }
        //进行授权注入sa-token权限框架
        StpUserUtil.login(yonghu.getId());
        return ResultView.ok(StpUserUtil.getTokenValue());
    }


    /**
     * 前端用户注册
     * @param account
     * @param mima
     * @return
     */
    @PostMapping("/register")
    public ResultView adLogin(@NotNull(message = "账号不能为空") String account,
                              @NotNull(message = "密码不能为空") String mima,
                              @NotNull(message = "昵称不能为空") String nickname,
                              @NotNull(message = "性别不能为空") String sex,
                              @NotNull(message = "联系方式不能为空") String lxfs){
        //进行账号查重认证
        boolean isOk=this.iYonghuService.exitAccount(account);
        if(isOk){
            return ResultView.error("用户账号已重复，请重新注册");
        }
        Yonghu yonghu=new Yonghu();
        yonghu.setAccount(account);
        yonghu.setMima(mima);
        yonghu.setNickname(nickname);
        yonghu.setSex(sex);
        yonghu.setLxfs(lxfs);
        yonghu.setUtime(new Date());
        yonghu.setCtime(new Date());
        boolean isSave=this.iYonghuService.save(yonghu);
        if(!isSave){
            return ResultView.error("注册异常");
        }
        return ResultView.ok();
    }


    /**
     * 获取当前用户信息
     */
    @GetMapping(value = "/getAccountInfo")
    public ResultView getAccountInfo() {
        String userId= StpUserUtil.getLoginIdAsString();
        Yonghu yonghu=this.iYonghuService.getById(userId);
        return ResultView.ok(yonghu);
    }





}
